import { Component, OnInit, ViewChild } from '@angular/core';
import { DatePipe } from '@angular/common';
import { NzMessageService } from 'ng-zorro-antd';
import { environment } from '../../../../environments/environment';
import { TableComponent } from './table.component';
import { Router, NavigationEnd } from '@angular/router';
import { ExportFileService } from '../../../core/exportFile';
import { _HttpClient } from '@delon/theme';
import { LocalStorage } from '@core/local.storage';
import { FarmReactComponent } from '../../../layout/components/farmreact.component';

@Component({
    selector: 'app-dailyschedule',
    templateUrl: './dailySchedule.component.html',
    styleUrls: ['./dailySchedule.component.less']
})

export class DailyScheduleComponent implements OnInit {
    @ViewChild('tcp', { static: true }) tcp: TableComponent;
    @ViewChild('farmReact', { static: false }) farmReact: FarmReactComponent;
    searchParam: any = {};
    apiUrl: any = {};
    tableScroll: any = {};
    exportUrl = '';
    headData = [];
    plainData = [];
    tempData = [];
    operaList: any = [
        { addModal: false },
        { delModal: false },
        { editModal: false },
        { tbModal: false },
        { approve: false }
    ];
    // workshopSelect = [];
    statusSelect = [];
    paramType = '';
    paramOver = '';
    tabIndex = 0;
    orgid = JSON.parse(this.ls.get('loginInfo')).VC_ORG$ID;
    level = JSON.parse(this.ls.get('loginInfo')).N_LEVEL;
    treeNodeStyle = { 'max-height': '300px', 'overflow-y': 'auto', 'overflow-x': 'hidden' };
    typeName = '';
    defaultdate = '';

    constructor(
        private exportFile: ExportFileService,
        private msg: NzMessageService,
        public ls: LocalStorage,
        private http: _HttpClient,
        private router: Router,
        private datePipe: DatePipe
    ) {
        this.router.events.subscribe((event: any) => {
            if (event instanceof NavigationEnd) {
                if (event.url === '/dailySchedule/workarea') {
                    this.loadList();
                }
            }
        });
    }

    ngOnInit() {
        this.typeName = window.location.href.split('/')[window.location.href.split('/').length - 1];
        this.headData = [
            { "name": "来源", "width": "6%" },
            { "name": "车站", "width": "8%" },
            { "name": "作业项目", "width": "12%" },
            { "name": "计划日期", "width": "6%" },
            { "name": "设备及分类" },

            { "name": "设备名称", "width": "10%" },
            { "name": "责任人", "width": "6%" },
            { "name": "是否上道", "width": "6%" },
            { "name": "是否天窗", "width": "6%" },
            { "name": "状态", "width": "8%" }
        ];
        this.statusSelect = [
            { opValue: '0', opName: '编制' },
            { opValue: '1', opName: '工区提交' },
            { opValue: '2', opName: '车间审核通过' },
            { opValue: '3', opName: '车间审核驳回' },
            { opValue: '4', opName: '段审批通过' },
            { opValue: '5', opName: '段审批驳回' }
        ];
        this.plainData = [
            "vcSource", "staName", "projectName", "dtWorkdate", "vcDevtype",
            "vcDevname", "vcCharger", "vcNeedroad", "vcNeekskywin", "vcStatus"
        ];
        this.apiUrl.add = environment.project + '/ymTWadayplankmn/add';
        this.apiUrl.del = environment.project + '/ymTWadayplankmn/delete';
        this.apiUrl.approve = environment.project + '/ymTWadayplankmn/update';
        this.apiUrl.edit = environment.project + '/ymTWadayplankmn/update';
        this.apiUrl.tb = environment.project + '/ymTWadayplankmn/update';
        this.searchParam.dtWorkdate = new Date().getTime() + 24 * 60 * 60 * 1000;
        this.loadList();
    }
    loadList() {
        let dtWorkdate = '';
        if (this.searchParam.dtWorkdate) {
            dtWorkdate = this.datePipe.transform(this.searchParam.dtWorkdate, 'yyyy-MM-dd');
        }
        switch (this.level) {
            case 10:
                this.tcp.loadList(environment.project + '/ymTWadayplankmn/query', '&vcOrgid=' + this.orgid + '&dtWorkdate=' + dtWorkdate);
                break;
            case 30:
                this.tcp.loadList(environment.project + '/ymTWadayplankmn/query', '&vcWsOrgId=' + this.orgid + '&dtWorkdate=' + dtWorkdate);
                break;
            default:
                this.tcp.loadList(environment.project + '/ymTWadayplankmn/query', '&dtWorkdate=' + dtWorkdate);
                break;
        }
    }

    search() {
        let params = '';
        if (this.searchParam.dtWorkdate) {
            params += '&dtWorkdate=' + this.datePipe.transform(this.searchParam.dtWorkdate, 'yyyy-MM-dd');
        }
        if (this.searchParam.vcStatus) {
            params += '&vcStatus=' + this.searchParam.vcStatus;
        }
        if (this.level === 10) {
            params += '&vcOrgid=' + this.orgid;
        } else if (this.level === 30) {
            params += '&vcWsOrgId=' + this.orgid;
        }
        this.farmReact.childOuter((res) => {
            if (res.chejianValue !== '0') {
                params += '&vcWsOrgId=' + (res.chejianValue || '');
            }
            if (res.gongquValue !== '0') {
                params += '&vcOrgid=' + (res.gongquValue || '');
            }
            if (res.chezhanValue !== '0') {
                params += '&vcStaid=' + (res.chezhanValue || '');
            }
        });
        this.tcp.loadList(environment.project + '/ymTWadayplankmn/query', params);
    }
    export() {
        let status = false;
        let count = 0;
        const vcId = [];
        for (const item of this.tcp.getAllData()) {
            if (item.checked) {
                count += 1;
                vcId.push(item.vcId);
                status = true;
            }
        }
        if (status && count === 1) {
            this.exportFile.getDown(environment.project + '/ymTWadayplankmn/export?vcId=' + vcId[0], '日计划.xls');
        } else {
            this.msg.error('请选择一条数据进行操作');
        }
    }
    modalShow(type) {
        this.tempData = this.tcp.getAllData();
        this.operaList[type] = true;
    }
    modalHide(type) {
        this.operaList[type] = false;
    }
}
